package com.geline.demo.web;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.geline.extension.domain.TableDataInfo;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 描述
 *
 * @author: mx
 * @date: 2024-06-05 17:58
 */
public class RequestInterceptor {

    public RequestInterceptor() {
    }

    public static HttpServletRequest getRequest() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attributes.getRequest();
    }

    public static HttpServletResponse getResponse() {
        ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        return attributes.getResponse();
    }

    public String renderString(String string) {
        try {
            HttpServletResponse response = getResponse();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().print(string);
        } catch (IOException var3) {
            IOException e = var3;
            e.printStackTrace();
        }

        return null;
    }

    public Map<String, Object> getConditionMap() {
        Map<String, Object> conditionMap = new HashMap();
        return conditionMap;
    }

    protected TableDataInfo getTableDataInfo(IPage page) {
        TableDataInfo tableDataInfo = new TableDataInfo(page.getCurrent(), page.getSize());
        tableDataInfo.setCode(0);
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setRows(page.getRecords());
        return tableDataInfo;
    }

    public String redirect(String url) {
        return StrUtil.format("redirect:{}", new Object[]{url});
    }
}
